package com.gxd.web.servlet;

import com.alibaba.fastjson.JSON;
import com.gxd.domain.GoodsType;
import com.gxd.service.GoodsTypeService;
import com.gxd.service.ShowGoodTypeService;
import com.gxd.service.impl.GoodsTypeServiceImpl;
import com.gxd.service.impl.ShowGoodTypeServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@WebServlet("/goodstypeservlet")
public class GoodsTypeServlet extends ServletBase {
    GoodsTypeService goodsTypeService = new GoodsTypeServiceImpl();
    ShowGoodTypeService showGoodTypeService = new ShowGoodTypeServiceImpl();




        public void goodstypelist(HttpServletRequest request, HttpServletResponse response) throws Exception {
         List<GoodsType> list= goodsTypeService.findByLevel(1);
        Object j = JSON.toJSON(list);

        response.setCharacterEncoding("utf-8");
        response.getWriter().print(j);


    }

    public String show(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<GoodsType> goodsTypeList=showGoodTypeService.findGoodType();
        request.setAttribute("goodsTypeList",goodsTypeList);
        return "forward:/admin/showGoodsType.jsp";
    }
    public String getGoodTypeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        List<GoodsType> goodsTypeList=showGoodTypeService.findGoodType();
        request.setAttribute("goodsTypeList",goodsTypeList);
        return "forward:/admin/addGoodsType.jsp";
    }
        public String add(HttpServletRequest request, HttpServletResponse response) throws Exception {


        int goodsParent =Integer.parseInt(request.getParameter("goodsParent")) ;
        String typename = request.getParameter("typename");
        GoodsType goodsType1=showGoodTypeService.selectLevel(goodsParent);

        GoodsType goodsType = new GoodsType();
        goodsType.setName(typename);
        goodsType.setParent(goodsParent);
        goodsType.setLevel(goodsType1.getLevel()+1);
        showGoodTypeService.addGoodType(goodsType);
        return "forward:/goodstypeservlet?method=show";
    }



    public String getUpdateGoodTypeList(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id =Integer.parseInt(request.getParameter("id"));
        GoodsType goodsType =showGoodTypeService.findById(id);
        List<GoodsType> goodsTypeList=showGoodTypeService.findGoodType();
        request.getSession().setAttribute("goodsType",goodsType);
        request.setAttribute("goodsTypeList",goodsTypeList);
        return "forward:/admin/updateGoodsType.jsp";
    }
    public String update(HttpServletRequest request, HttpServletResponse response) throws Exception {


        int goodsParent =Integer.parseInt(request.getParameter("goodsParent")) ;
        int id =Integer.parseInt(request.getParameter("id")) ;

        String typename = request.getParameter("typename");
        GoodsType goodsType1=showGoodTypeService.selectLevel(goodsParent);

        GoodsType goodsType = new GoodsType();
        goodsType.setId(id);
        goodsType.setName(typename);
        goodsType.setParent(goodsParent);
        goodsType.setLevel(goodsType1.getLevel()+1);
        showGoodTypeService.update(goodsType);
        return "forward:/goodstypeservlet?method=show";
    }









    public String deleteGoodType(HttpServletRequest request, HttpServletResponse response) throws Exception {
        int id= Integer.parseInt(request.getParameter("id"));
        showGoodTypeService.deleteById(id);

        return "forward:/goodstypeservlet?method=show";
    }
}
